package com.stox.client.ui.javafx.screener.widget;

import javafx.collections.FXCollections;
import javafx.scene.Node;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;

import com.stox.client.api.core.Client;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.widget.NumberSpinner;
import com.stox.client.ui.javafx.widget.ScripCriteriaPanel;
import com.stox.shared.core.i18n.MessageSource;
import com.stox.shared.core.model.Exchange;
import com.stox.shared.core.model.enumeration.BarSpan;
import com.stox.shared.core.model.filter.ScripCriteria;
import com.stox.shared.screener.model.ScreenerConfiguration;

public class ScreenerConfigurationPanel extends VBox {

	private final ScripCriteriaPanel scripCriteriaPanel = new ScripCriteriaPanel();
	private final NumberSpinner matchSpanNumberSpinner = new NumberSpinner();
	private final NumberSpinner offsetNumberSpinner = new NumberSpinner();
	private final ComboBox<BarSpan> barSpanComboBox = new ComboBox<BarSpan>(FXCollections.observableArrayList(BarSpan.values()));

	public ScreenerConfigurationPanel() {
		UI.style(this, CSS.beanAutoUI);

		getChildren().addAll(scripCriteriaPanel, createHBox("Bar.Type", barSpanComboBox), createHBox("Number.Of.Bars.To.Match", matchSpanNumberSpinner), createHBox("Number.Of.Bars.To.Offset", offsetNumberSpinner));
	}

	public void value(final ScreenerConfiguration screenerConfiguration) {
		screenerConfiguration.setBarType(barSpanComboBox.getValue());
		screenerConfiguration.setMatchSpan(matchSpanNumberSpinner.getValue().intValue());
		screenerConfiguration.setOffset(offsetNumberSpinner.getValue().intValue());
		scripCriteriaPanel.value(screenerConfiguration.getScripCriteria());
	}

	private HBox createHBox(final String key, final Node widget) {
		final HBox hBox = new HBox();
		UI.style(hBox, CSS.propertyAutoUI);

		final Pane labelPane = new Pane();
		UI.style(labelPane, CSS.propertyAutoUILabelPane);
		final Label label = new Label(MessageSource.get(key));
		HBox.setHgrow(label, Priority.ALWAYS);
		labelPane.getChildren().add(label);
		HBox.setHgrow(labelPane, Priority.ALWAYS);

		final Pane widgetPane = new Pane();
		UI.style(widgetPane, CSS.propertyAutoUIWidgetPane);
		widgetPane.getChildren().add(widget);
		HBox.setHgrow(widgetPane, Priority.ALWAYS);
		UI.style(widget, CSS.propertyAutoUIWidget);

		hBox.getChildren().addAll(labelPane, widgetPane);
		return hBox;
	}

	public void populate(final ScreenerConfiguration screenerConfiguration) {
		if (null != screenerConfiguration.getScripCriteria()) {
			final ScripCriteria scripCriteria = screenerConfiguration.getScripCriteria();
			if (null != scripCriteria.getExchangeId()) {
				final Exchange exchange = Client.EXCHANGE.findOne(scripCriteria.getExchangeId());
				if (null != exchange) {
					scripCriteriaPanel.getExchangeComboBox().getSelectionModel().select(exchange);
				}
			}
			if (null != scripCriteria.getScripType()) {
				scripCriteriaPanel.getScripTypeComboBox().getSelectionModel().select(scripCriteria.getScripType());
			}
		}
		matchSpanNumberSpinner.setValue(screenerConfiguration.getMatchSpan());
		offsetNumberSpinner.setValue(screenerConfiguration.getOffset());
		if (null != screenerConfiguration.getBarType()) {
			barSpanComboBox.getSelectionModel().select(screenerConfiguration.getBarType());
		}
	}

}
